<?php
$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data User</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=user" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="user">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="nama" <?php echo ($field=="nama")?"selected":"";?>>Nama</option>
									<option value="username" <?php echo ($field=="username")?"selected":"";?>>Username</option>
									<option value="jabatan" <?php echo ($field=="jabatan")?"selected":"";?>>Jabatan</option>																	
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Nama User</th>
									<th>Username</th>
									<th>Jabatan</th>									
									<th class="action" colspan="2"><a href="?pages=user&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=2;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}				
							$sqlUser = mysql_query("SELECT * FROM tb_user $addQuery ORDER BY id_user ASC LIMIT $posisi, $batas");
							
							$no=$posisi+1;
							while($dataUser = mysql_fetch_array($sqlUser)){								

								//$status = ($dataUser['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td><?php echo $dataUser['nama'];?></td>
									<td><?php echo $dataUser['username'];?></td>
									<td><?php echo $dataUser['jabatan'];?></td>																
									<td class="action"><a href="?pages=user&act=edit&id=<?php echo $dataUser['id_user'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=user&act=del&id=<?php echo $dataUser['id_user'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlUser2 = mysql_query("SELECT * FROM tb_user $addQuery ORDER BY id_user ASC");
							$jumlahData = mysql_num_rows($sqlUser2);

							$file="?pages=user&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_user = (int) filter($_GET['id']);

	$sqlUser = mysql_query("SELECT * FROM tb_user WHERE id_user=$id_user");
	$dataUser = mysql_fetch_array($sqlUser);

	if(!empty($id_user)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data User</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data User</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=user&act=update">
		<input type="hidden" name="id_user" value="<?php echo $dataUser['id_user'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Nama</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataUser['nama'];?>" size="60" name="nama" id="nama" placeholder="Nama" required></td>
			   	</tr>
			   	<tr>
					<td><label>Username</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataUser['username'];?>" size="60" name="username" id="username" placeholder="Username" required></td>
			   	</tr>
			   	<tr>
					<td><label>Password</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataUser['password'];?>" size="60" name="password" id="password" placeholder="Password" required></td>
			   	</tr>
			   	<tr>
					<td><label>Jabatan</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataUser['jabatan'];?>" size="60" name="jabatan" id="jabatan" placeholder="Jabatan" required></td>
			   	</tr>
				<tr>
					<td><label>User Sales</label></td>
					<td>:</td>
					<td>
						<select name="status" style="width:202px">
							<option value="0"> -- Pilih Sales -- </option>
							<?php
							$select_jur = mysql_query("SELECT * FROM tb_sales");
							while($select_jur2 = mysql_fetch_array($select_jur)){
							if($select_jur2['id_sales']==$dataUser['id_sales']){
								$selected = " selected ";
							}else{
								$selected = "";
							}
							?>
							<option <?php echo $selected;?> value="<?php echo"$select_jur2[id_sales]";?>"><?php echo"$select_jur2[nama]";?></option>
							<?php
							}
							?>
						</select>
					</td>
			   	</tr>
			 	<tr>
			 	  <td colspan="3">
                  <table width="704">
                  	<tbody>
                    	<tr>                        	
                    		<th class="selected"><input type="checkbox"/></th> 
                            <th>Master</th>                 	
                            <th class="selected"><input type="checkbox"/></th>
                            <th>Bahan Baku</th>
                            <th class="selected"><input type="checkbox"/></th>
                            <th>Produksi</th>
                            <th class="selected"><input type="checkbox"/></th>
                            <th>Penjualan</th>
                            <th class="selected"><input type="checkbox"/></th>
                            <th>Penjadwalan</th>
                        </tr>
                  	</tbody>
                  	<tbody>
                    	<tr>                        	
                    		<td class="selected"><input type="checkbox"/></td> 
                            <td>Suplier</td>                 	
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Bahan Baku</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Produksi</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjualan</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjadwalan</td>
                        </tr>
                  	</tbody>
                  	<tbody>
                    	<tr>                        	
                    		<td class="selected"><input type="checkbox"/></td> 
                            <td>Customer</td>                 	
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Bahan Baku</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Produksi</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjualan</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjadwalan</td>
                        </tr>
                  	</tbody>  
                  	<tbody>
                    	<tr>                        	
                    		<td class="selected"><input type="checkbox"/></td> 
                            <td>Sales</td>                 	
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Bahan Baku</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Produksi</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjualan</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjadwalan</td>
                        </tr>
                  	</tbody> 
                  	<tbody>
                    	<tr>                        	
                    		<td class="selected"><input type="checkbox"/></td> 
                            <td>User</td>                 	
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Bahan Baku</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Produksi</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjualan</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjadwalan</td>
                        </tr>
                  	</tbody> 
                  	<tbody>
                    	<tr>                        	
                    		<td class="selected">&nbsp;</td> 
                            <td>&nbsp;</td>                 	
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Bahan Baku</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Produksi</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjualan</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjadwalan</td>
                        </tr>
                  	</tbody> 
                  	<tbody>
                    	<tr>                        	
                    		<td class="selected">&nbsp;</td> 
                            <td>&nbsp;</td>                 	
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Bahan Baku</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Produksi</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjualan</td>
                            <td class="selected"><input type="checkbox"/></td>
                            <td>Penjadwalan</td>
                        </tr>
                  	</tbody>                   
                  </table>
				  <table style="width:auto">
		   	<tbody>
		   		<tr> <!--<input type="checkbox" name="cadmin2" value="checkbox" <?php if (substr($dataUser['akses'],12,1)=='1'){ echo "checked";}?> />-->
					<th class="selected"><input type="checkbox" name="m1" value="checkbox" <?php if (substr($dataUser['akses'],1,1)=='1'){ echo "checked";}?>/></th>
					<th>Master</th>
					<th class="selected"><input type="checkbox" name="bb1" value="checkbox" <?php if (substr($dataUser['akses'],6,1)=='1'){ echo "checked";}?>/></th>
					<th>Bahan Baku </th>
					<th class="selected"><input type="checkbox" name="pdk1" value="checkbox" <?php if (substr($dataUser['akses'],10,1)=='1'){ echo "checked";}?>/></th>
					<th>Produksi</th>
					<th class="selected"><input type="checkbox" name="pjl1" value="checkbox" <?php if (substr($dataUser['akses'],16,1)=='1'){ echo "checked";}?>/></th>
					<th>Penjualan</th>
					<th class="selected"><input type="checkbox" name="pjw1" value="checkbox" <?php if (substr($dataUser['akses'],20,1)=='1'){ echo "checked";}?>/></th>
					<th>Penjadwalan</th>
		   		</tr>
			   	<tr>
					<td class="selected"><input type="checkbox" name="m2" value="checkbox" <?php if (substr($dataUser['akses'],2,1)=='1'){ echo "checked";}?>/></td>
					<td>Suplier</td>
					<td class="selected"><input type="checkbox" name="bb2" value="checkbox" <?php if (substr($dataUser['akses'],7,1)=='1'){ echo "checked";}?>/></td>
					<td>Bahan Baku Masuk </td>
					<td class="selected"><input type="checkbox" name="pdk2" value="checkbox" <?php if (substr($dataUser['akses'],11,1)=='1'){ echo "checked";}?>/></td>
					<td>Formula Produksi </td>
					<td class="selected"><input type="checkbox" name="pjl2" value="checkbox" <?php if (substr($dataUser['akses'],17,1)=='1'){ echo "checked";}?>/></td>
					<td>Barang Keluar </td>
					<td><input type="checkbox" name="pjw2" value="checkbox" <?php if (substr($dataUser['akses'],21,1)=='1'){ echo "checked";}?>/></td>
					<td>Pengaturan Jadwal</td>
			   	</tr>
			   	<tr>
				  <td class="selected"><input type="checkbox" name="m3" value="checkbox" <?php if (substr($dataUser['akses'],3,1)=='1'){ echo "checked";}?>/></td>
					<td>Customer</td>
					<td class="selected"><input type="checkbox" name="bb3" value="checkbox" <?php if (substr($dataUser['akses'],8,1)=='1'){ echo "checked";}?>/></td>
					<td>Bahan Baku Keluar </td>
					<td class="selected"><input type="checkbox" name="pdk3" value="checkbox" <?php if (substr($dataUser['akses'],12,1)=='1'){ echo "checked";}?>/></td>
					<td>Proses Produksi </td>
					<td class="selected"><input type="checkbox" name="pjl3" value="checkbox" <?php if (substr($dataUser['akses'],18,1)=='1'){ echo "checked";}?>/></td>
					<td>Barang Reject </td>
					<td><input type="checkbox" name="pjw3" value="checkbox" <?php if (substr($dataUser['akses'],22,1)=='1'){ echo "checked";}?>/></td>
					<td>Jadwal Saya</td>
			   	</tr>
			   	<tr>
				  <td class="selected"><input type="checkbox" name="m4" value="checkbox" <?php if (substr($dataUser['akses'],4,1)=='1'){ echo "checked";}?>/></td>
					<td>Sales</td>
					<td class="selected"><input type="checkbox" name="bb4" value="checkbox" <?php if (substr($dataUser['akses'],9,1)=='1'){ echo "checked";}?>/></td>
					<td>Bahan Baku Emergency</td>
					<td class="selected"><input type="checkbox" name="pdk4" value="checkbox" <?php if (substr($dataUser['akses'],13,1)=='1'){ echo "checked";}?>/></td>
					<td>Ready To Use </td>
					<td class="selected"><input type="checkbox" name="pjl4" value="checkbox" <?php if (substr($dataUser['akses'],19,1)=='1'){ echo "checked";}?>/></td>
					<td>Barang Perbaikan </td>
					<td><input type="checkbox" name="pjw4" value="checkbox" <?php if (substr($dataUser['akses'],23,1)=='1'){ echo "checked";}?>/></td>
					<td>Histori Jadwal</td>
			   	</tr>			   	
			 	<tr>
			 	  <td class="selected"><input type="checkbox" name="m5" value="checkbox" <?php if (substr($dataUser['akses'],5,1)=='1'){ echo "checked";}?>/></td>
			 	  <td>User</td>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
			 	  <td class="selected"><input type="checkbox" name="pdk5" value="checkbox" <?php if (substr($dataUser['akses'],14,1)=='1'){ echo "checked";}?>/></td>
			 	  <td>Ready To Sale </td>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
			 	  <td><input type="checkbox" name="pjw5" value="checkbox" <?php if (substr($dataUser['akses'],24,1)=='1'){ echo "checked";}?>/></td>
			 	  <td>Jadwal Hari Ini</td>
		 	  </tr>
			 	<tr>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
			 	  <td class="selected"><input type="checkbox" name="pdk6" value="checkbox" <?php if (substr($dataUser['akses'],15,1)=='1'){ echo "checked";}?>/></td>
			 	  <td>Emergency Stock </td>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
			 	  <td>&nbsp;</td>
		 	  </tr>
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>				  </td>
		 	  </tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_user = $_POST['id_user'];
	$nama = $_POST['nama'];
	$username = $_POST['username'];
	$password = $_POST['password'];
	$jabatan = $_POST['jabatan'];	
	$idsales = $_POST['status'];	
	
	$aks="0";
  	if ($_POST['m1']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['m2']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['m3']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['m4']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['m5']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	
	if ($_POST['bb1']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['bb2']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['bb3']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['bb4']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	
	if ($_POST['pdk1']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pdk2']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pdk3']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pdk4']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pdk5']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pdk6']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	
	if ($_POST['pjl1']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pjl2']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pjl3']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pjl4']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	
	if ($_POST['pjw1']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pjw2']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pjw3']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pjw4']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	if ($_POST['pjw5']=="checkbox") { $aks=$aks."1";} else { $aks=$aks."0"; }
	

	if(!empty($id_user)){
		$update = mysql_query("UPDATE tb_user SET nama='$nama', username='$username', password='$password', jabatan='$jabatan', akses='$aks', id_sales='$idsales' WHERE id_user=$id_user");

		if($update)
			sukses('Data berhasil disimpan', '?pages=user');
	}else{
		$insert = mysql_query("INSERT INTO tb_user (id_user,nama,username,password,jabatan,akses,id_sales) VALUES(NULL, '$nama', '$username', '$password', '$jabatan', '$aks', '$idsales')");

		if($insert)
			sukses('Data berhasil disimpan', '?pages=user');
	}

}elseif ($act=='del') {
	# code...
	$id_user = (int) filter($_GET['id']);

	$delete = mysql_query("DELETE FROM tb_user WHERE id_user=$id_user");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=user');
}
?>				